package com.example.excel.base;

import android.os.Bundle;
import android.text.TextUtils;

import com.example.excel.config.Constants;
import com.example.excel.ui.activity.login.LoginActivity;
import com.example.excel.utils.LoadingDialog;
import com.example.excel.utils.SharedPreferencesUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

public abstract class BaseActivity<D extends ViewDataBinding, T extends BaseViewModel> extends AppCompatActivity {

    protected D mBinding;
    protected T mViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBundle(savedInstanceState);
        Class<T> tClassModel = initViewModel();
        if (tClassModel != null) {
            mViewModel = ViewModelProviders.of(this).get(tClassModel);
        }
        mBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mBinding.setLifecycleOwner(this);
        initView();
        registerObserve();
    }

    protected void initBundle(Bundle savedInstanceState) {

    }

    protected abstract Class<T> initViewModel();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected void registerObserve() {
        if (mViewModel == null) return;
        mViewModel.getDialog().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    showLoadingDialog("");
                } else {
                    hideLoadingDialog();
                }
            }
        });
        mViewModel.getTxtDialog().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (!TextUtils.isEmpty(s)) {
                    showLoadingDialog(s);
                } else {
                    hideLoadingDialog();
                }
            }
        });
        mViewModel.getJumpLogin().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    SharedPreferencesUtils.setString(Constants.LOGIN_TOKEN, "");
                    LoginActivity.start(BaseActivity.this);
                }
            }
        });
    }

    private LoadingDialog mLoadingDialog;

    public void showLoadingDialog(String title) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this, title);
        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
        super.onDestroy();
    }
}
